<?php 
namespace Common\Common; 
class CCreditCard{  

 // Class Members  
 var $__ccNum = '';  
  // Constructor   
 function CCreditCard($num){
  // Set member variables      
  $this->__ccNum = $num;    

 }

 function SafeNumber($char = 'x', $numToHide = 4){     
   // Return only part of the number     
   if($numToHide < 4){     
     $numToHide = 4;     
   }    

   if($numToHide > 10){     
     $numToHide = 10;     
   }     

   $cardNumber = $this->__ccNum;     
   $cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);        

   for($i = 0; $i < $numToHide; $i++){     
     $cardNumber .= $char;     
   }     

   return $cardNumber;     
 }

 function IsValid(){     

   // Not valid by default      
   $passCheck = false;
   
 // Is the number in the correct format?     
      if(ereg("^5[1-5][0-9]{14}$", $this->__ccNum))
		 $ccType="Master Card";
	  elseif(ereg("^4[0-9]{12}([0-9]{3})?$", $this->__ccNum))
		 $ccType="Visa Card";
	  elseif(ereg("^3[47][0-9]{13}$", $this->__ccNum))
		 $ccType="American Express";
	  elseif(ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this->__ccNum))
		 $ccType="Discover";
	  elseif(ereg("^6011[0-9]{12}$", $this->__ccNum))
		 $ccType="Diners Club";
	  elseif(ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__ccNum))
		 $ccType="JCB";
	  else
	    $ccType=0; 
		

 // Is the number valid?      
 $cardNumber = strrev($this->__ccNum);      
 $numSum = 0;      
 for($i = 0; $i < strlen($cardNumber); $i++){
   $currentNum = substr($cardNumber, $i, 1);        
 // Double every second digit      
 if($i % 2 == 1){      
   $currentNum *= 2;      
 }        
 // Add digits of 2-digit numbers together      
 if($currentNum > 9){      
   $firstNum = $currentNum % 10;      
   $secondNum = ($currentNum - $firstNum) / 10;      
   $currentNum = $firstNum + $secondNum;      
 }      
 $numSum += $currentNum;      

}
  // If the total has no remainder it's OK      
   $passCheck = ($numSum % 10 == 0); 
 
   if($ccType && $passCheck){
    $data['status']=1;
    $data['info']=$ccType;
   }else{
   	$data['status']=0;
	$data['info']="Unknown credit card type";
   }     
return $data;
}      
   
}
?>